﻿//NorManager - OpenTTD Dedicated server manager
//Copyright (C) 2014 Tomáš Bosek

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Text;
using System.IO;

namespace NorManager
{
    /// <summary>
    /// Looks for saved games.
    /// </summary>
    class SaveManager
    {
        private string savepath = null;

        internal SaveManager(string path)
        {
            if (path != String.Empty)
                savepath = Path.GetDirectoryName(path);
            
            savepath = Path.Combine(savepath, "save");
        }

        /// <summary>
        /// Finds last game save.
        /// </summary>
        /// <returns>Last saved game path or String.Empty.</returns>
        internal string FindLastSave()
        {
            //All saves
            string[] save = new string[0];
            //Autosaves only
            string[] autosave = new string[0];
            //"Handmade" saves only
            string[] saves = new string[0];

            Logger.Instance.Log(savepath);
            if (Directory.Exists(savepath))
            {
                string savedir = savepath;
                //First get all "handmade" saves
                save = Directory.GetFiles(savedir, "*.sav");
                //Then get all autosaves
                if (Directory.Exists(Path.Combine(savedir, "autosave/")))
                    autosave = Directory.GetFiles(Path.Combine(savedir, "autosave"), "*.sav");
            }
            else
                return String.Empty;

            //Merge arrays to one
            saves = new string[save.Length + autosave.Length];
            Array.Copy(save, 0, saves, 0, save.Length);
            Array.Copy(autosave, 0, saves, save.Length, autosave.Length);

            //String with lastsave path
            string lastsave = null;
            //Lastsave DateTime
            DateTime lastsaveTime = DateTime.Now;
            for (int i = 0; i < saves.Length; i++)
            {
                if (lastsave == null)
                {
                    lastsave = saves[i];
                    lastsaveTime = File.GetLastWriteTime(lastsave);
                    continue;
                }

                DateTime fileTime = File.GetLastWriteTime(saves[i]);
                if (fileTime > lastsaveTime)
                {
                    lastsave = saves[i];
                    lastsaveTime = fileTime;
                }
            }
            //Convert absolute path to relative
            lastsave = lastsave.Replace(savepath, String.Empty);

            return lastsave;
        }
    }
}
